﻿#define _CRT_SECURE_NO_WARNINGS 1 
#include"contact.h"

//初始化通讯录
void InitContact(Contact* pcon)
{
	assert(pcon);
	pcon->sz = 0;
	pcon->data = (PeoInfo*)calloc(DEFAULT_SZ, sizeof(PeoInfo));
	if (pcon->data == NULL)
	{
		printf("%s\n", strerror(errno));
		return;
	}
	pcon->capacity = DEFAULT_SZ;

	LoadContact(pcon); //初始化时直接读取

}

//增加联系人
void AddContact(Contact* pcon)
{
	assert(pcon);

	/******新增代码******/
	CheckCapacity(pcon); //插入前检查扩容情况
	/******************/

	//录入信息
	printf("请输入名字:>");
	scanf("%s", pcon->data[pcon->sz].name);
	printf("请输入年龄:>");
	scanf("%d", &(pcon->data[pcon->sz].age));
	printf("请输入性别:>");
	scanf("%s", pcon->data[pcon->sz].sex);
	printf("请输入电话:>");
	scanf("%s", pcon->data[pcon->sz].tele);
	printf("请输入地址:>");
	scanf("%s", pcon->data[pcon->sz].addr);
	;
	pcon->sz++;
	printf("增加成功\n");
}



//显示所有联系人的信息
void ShowContact(const Contact* pc)
{
	assert(pc);
	//打印列标题
	printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	//打印数据
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n",
			pc->data[i].name,
			pc->data[i].age,
			pc->data[i].sex, 
			pc->data[i].tele, 
			pc->data[i].addr);
	}

}

static int FindByName(const Contact* pc, char name[])
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;//找到
		}
	}
	return -1;//没找到
}


//删除指定联系人
void DelContact(Contact* pc)
{
	char name[MAX_NAME];
	assert(pc);

	printf("请输入要删除的人名:>");
	scanf("%s", name);

	//找到要删除的人
	/*int i = 0;
	int del = 0;
	int flag = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			del = i;
			flag = 1;
			break;
		}
	}
	if (flag == 0)
	{
		printf("要删除的人不存在\n");
		return;
	}*/
	int i=0;
	int del = FindByName(pc, name);
	if (del == -1)
	{
		printf("要删除的人不存在\n");
		return;
	}

	//删除坐标为del的联系人
	for (i = del; i < pc->sz - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->sz--;
	printf("成功删除联系人\n");
}

//查找指定联系人
void SearchContact(Contact* pc)
{
	assert(pc);
	char name[MAX_NAME];
	printf("请输入要查找人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc,name);
	if (pos == -1)
	{
		printf("要查找的联系人不存在\n");
	}
	else
	{
		printf("%-10s\t%-4s\t%-5s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");

		printf("%-10s\t%-4d\t%-5s\t%-12s\t%-30s\n",
				pc->data[pos].name,
				pc->data[pos].age,
				pc->data[pos].sex,
				pc->data[pos].tele,
				pc->data[pos].addr);
		
	}
}

//更改指定联系人的信息
void ModifyContact(const Contact* pc)
{
	assert(pc);
	char name[MAX_NAME];
	printf("请输入要更改人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要更改的联系人不存在\n");
	}
	else
	{
		printf("请输入名字:>");
		scanf("%s", pc->data[pc->sz].name);
		printf("请输入年龄:>");
		scanf("%d", &(pc->data[pc->sz].age));
		printf("请输入性别:>");
		scanf("%s", pc->data[pc->sz].sex);
		printf("请输入电话:>");
		scanf("%s", pc->data[pc->sz].tele);
		printf("请输入地址:>");
		scanf("%s", pc->data[pc->sz].addr);

		printf("成功更改联系人信息\n");

	}
}

//排序
void SortContact(Contact* pc)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < pc->sz; i++)
	{
		struct PeoInfo exchange;
		for (j = 0; j < pc->sz - 1 - i; j++)
		{
			if (strcmp(pc->data[j].name, pc->data[j + 1].name))
			{
				exchange = pc->data[j];
				pc->data[j] = pc->data[j + 1];
				pc->data[j + 1] = exchange;
			}
		}
	}
	printf("排序成功\n");
}

//删除所有联系人
//void ClearAll(Contact* pc)
//{
//	for (int i = 0; i < pc->sz - 1; i++)
//	{
//		free(pc->data[pc->sz].name);
//		free(pc->data[pc->sz].age);
//		free(pc->data[pc->sz].sex);
//		free(pc->data[pc->sz].tele);
//		free(pc->data[pc->sz].addr);
//	}
//	pc->sz--;
//	printf("成功删除所有联系人\n");
//}



void CheckCapacity(Contact* pcon)
{
	if (pcon->sz == pcon->capacity)
	{
		//每次扩容双倍
		PeoInfo* ptr = realloc(pcon->data, (pcon->capacity * 2) * sizeof(PeoInfo));
		if (ptr != NULL)
		{
			pcon->data = ptr;
			pcon->capacity *= 2;
			printf("增容成功\n");
		}
	}
}
​;
void DestroyContact(Contact* pcon)
{
	free(pcon->data);
	pcon->data = NULL;
	pcon->capacity = 0;
	pcon->sz = 0;
}


void LoadContact(Contact* pcon)
{
	PeoInfo tmp = { 0 };
	FILE* pfRead = fopen("contact.dat", "rb");
	if (pfRead == NULL)
	{
		printf("加载信息：打开文件失败\n");
		return;
	}
	//加载信息
	while (fread(&tmp, sizeof(PeoInfo), 1, pfRead))
	{
		CheckCapacity(pcon);
		pcon->data[pcon->sz] = tmp;
		pcon->sz++;
	}
	​;
	fclose(pfRead);
	pfRead = NULL;
}
; ​;
void SaveContact(Contact* pcon)
{
	int i = 0;
	FILE* pfWrite = fopen("contact.dat", "wb");
	if (pfWrite == NULL)
	{
		printf("保存信息：打开文件失败\n");
		return;
	}
	//保存信息
	for (i = 0; i < pcon->sz; i++)
	{
		fwrite(pcon->data + i, sizeof(PeoInfo), 1, pfWrite);
	}
	​;
		//关闭文件
		fclose(pfWrite);
	pfWrite = NULL;
}